package oa.hleast.xswl.service.activity.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.activity.CmsUserSignInMapper;
import oa.hleast.xswl.pojo.domain.activity.CmsUserSignIn;
import oa.hleast.xswl.service.activity.ICmsUserSignInService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
@AllArgsConstructor
public class CmsActivitySignInServiceImpl extends ServiceImpl<CmsUserSignInMapper, CmsUserSignIn> implements ICmsUserSignInService {


    @Override
    public Integer countSignInByCalendar(Long calendarId) {

        return this.count(new LambdaQueryWrapper<CmsUserSignIn>()
                .eq(CmsUserSignIn::getCalendarId, calendarId)
        );
    }

    /**
     * 某天某用户的签到情况
     * 没有签到返回 null
     *
     * @param userId     用户ID
     * @param calendarId 日历ID/场次ID
     */
    @Override
    public CmsUserSignIn signInByCalendarAndUserId(Long userId, Long calendarId) {
        List<CmsUserSignIn> userSignInList = this.list(new LambdaQueryWrapper<CmsUserSignIn>().eq(CmsUserSignIn::getUserId, userId).eq(CmsUserSignIn::getCalendarId, calendarId).last("limit 1"));
        if (userSignInList.size() != 1) {
            return null;
        }
        return userSignInList.get(0);
    }

    /**
     * 某场次某用户的是否签到
     */
    @Override
    public boolean isSignInByCalendarAndUserId(Long userId, Long calendarId) {
        int count = this.count(new LambdaQueryWrapper<CmsUserSignIn>().eq(CmsUserSignIn::getUserId, userId).eq(CmsUserSignIn::getCalendarId, calendarId).last("limit 1"));
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean add(CmsUserSignIn form) {
        return this.save(form);
    }

    @Override
    public boolean update(CmsUserSignIn form) {
        return this.update(form);
    }

    @Override
    public Page<CmsUserSignIn> pageList(Integer page, Integer limit, String typeName, Integer display) {
        return this.page(new Page<>(page, limit), new LambdaQueryWrapper<CmsUserSignIn>()
                .orderByDesc(CmsUserSignIn::getGmtCreate)
                .orderByDesc(CmsUserSignIn::getGmtModified)
        );
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        return this.removeByIds(asList);
    }

    @Override
    public CmsUserSignIn detail(Long id) {
        return this.getById(id);
    }


}
